package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
@Slf4j
@RestController
@Api(tags = "01.通知管理模块")
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @ApiOperationSupport(order=10)
    @ApiOperation("新增公告")
    @PostMapping("add")
    //@ResponseBody
    public JsonResult addNotice(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam:"+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
        /*
        梳理业务逻辑,确定接口;
        1.接收客户端提交的数据
        2.校验合法性
        3.确定用户名是否被占用
         */
        return JsonResult.ok();
    }

    @ApiOperationSupport(order=20)
    @ApiOperation("查询公告")
    @GetMapping("list")
    public JsonResult noticeList(@ApiIgnore NoticeListQuery noticeListQuery){
        log.debug("noticeListQuery:"+noticeListQuery);
        String title = noticeListQuery.getTitle();
        Integer type = noticeListQuery.getType();
        log.debug("title:"+title);
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNotice(title, type);
        return JsonResult.ok(noticeListVOS);
    }

    @ApiOperationSupport(order=30)
    @ApiOperation("公告详情")
    @GetMapping("detail")
    public JsonResult detail(Long id){
        log.debug("id:"+id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);
        return JsonResult.ok(noticeDetailInfoVO);

    }


    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value="通知编号",required = true,dataType = "Long"),
        @ApiImplicitParam(name="title",value="公告标题",required = true,dataType = "String")
    })
    @ApiOperationSupport(order=40)
    @ApiOperation("删除公告")

    @PostMapping("del")
    public JsonResult delNotice(Long id,String title){
        log.debug("id:"+id);
        noticeMapper.delNotice(id);
        return JsonResult.ok();
    }


    @ApiOperationSupport(order=50)
    @ApiOperation("修改公告")
    @PostMapping("update")
    public JsonResult updateNotice(NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam:"+noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        log.debug("notice:"+notice);
        noticeMapper.updateNotice(notice);
        return JsonResult.ok();
    }
}
